/*
 * @Author: 余文童、万仁杰
 * @Date: 2020-08-10 10:53:10
 * @LastEditTime: 2020-08-13 01:33:27
 * @LastEditors: 余文童
 * @Description: 用户控制器类
 * @FilePath: \BackEnd\saleflower\src\main\java\com\wangwang\saleflower\controller\UserController.java
 */
package com.wangwang.saleflower.controller;

import com.wangwang.saleflower.entity.ResultState;
import com.wangwang.saleflower.entity.User;
import com.wangwang.saleflower.entity.ResultState.State;
import com.wangwang.saleflower.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;

@RestController
@RequestMapping("/api")
public class UserController {
    @Autowired
    UserService userService;

    static final String[] errStr = { "用户不存在" };

    @GetMapping(value = "/users")
    public Object[] selectUserList() {
        return userService.selectUserList().toArray();
    }

    @PostMapping(value = "/register")
    @ResponseBody
    public ResultState register(@RequestParam(name = "userid") String userId,
            @RequestParam(name = "username") String userName, @RequestParam(name = "userpw") String password) {
        User user = new User();
        ResultState result;
        if (userService.isUserExist(userName)) {
            result = ResultState.CreateResult(State.ERROR, null, "用户已经存在");
        } else {
            user.setUserid(userId);
            user.setUsername(userName);
            user.setUserpw(password);
            userService.saveUser(user);
            result = ResultState.CreateResult(State.SUCCESS, user, "成功添加用户");
        }
        return result;
    }

    @GetMapping(value = "/user")
    @ResponseBody
    public ResultState getUserByName(@RequestParam(name = "userid") String userName,
            @RequestParam(name = "userpw") String password) {
        ResultState result;
        var user = userService.getUserByName(userName);
        if (user != null && user.getUserpw().compareTo(password) == 0) {
            result = ResultState.CreateResult(State.SUCCESS, user, "成功");
        } else {
            result = ResultState.CreateResult(State.ERROR, null, errStr[0]);
        }
        return result;
    }

    @GetMapping(value = "/user/{userName}")
    public ResultState getUser(@PathVariable(value = "userName") String userName) {
        ResultState result;
        if (userService.isUserExist(userName)) {
            result = ResultState.CreateResult(State.SUCCESS, userService.getUserByName(userName), "成功");
        } else {
            result = ResultState.CreateResult(State.ERROR, null, "用户不存在");
        }
        return result;
    }

    @PostMapping("/users/isuserexist")
    @ResponseBody
    public ResultState isUserExist(@RequestParam(name = "userid") String userName) {
        ResultState result;
        if (userService.isUserExist(userName)) {
            result = ResultState.CreateResult(State.SUCCESS, userName, "成功查找到用户");
        } else {
            result = ResultState.CreateResult(State.ERROR, null, errStr[0]);
        }
        return result;
    }

    @PutMapping("/{userName}/settings")
    public ResultState editUser(@PathVariable(value = "userName") String userName, @RequestBody User user) {
        userService.updateUserInfo(user);
        return ResultState.CreateResult(State.SUCCESS, user, "修改成功");
    }

    @DeleteMapping("/delete")
    public ResultState removeUser(@RequestBody String userName) {
        ResultState result;
        if (userService.isUserExist(userName)) {
            userService.removeUserByName(userName);
            result = ResultState.CreateResult(State.SUCCESS, userName, "注销用户成功");
        } else {
            result = ResultState.CreateResult(State.ERROR, userName, errStr[0]);
        }
        return result;
    }
}
